<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>网上购票</title>
<link rel="stylesheet" href="resources/css/frozen.css">
<script src="resources/lib/zepto.min.js"></script>
<script src="resources/js/frozen.js"></script>
<script src="resources/js/config.js"></script>
<script src="resources/js/main.js"></script>

</head>
<body ontouchstart="">
	<header class="ui-header ui-header-positive ui-border-b">
		<i class="ui-icon-return" onclick="location='index.html'"></i>
		<h1>选择起点站</h1>
	</header>
	<footer class="ui-footer ui-footer-btn">
		<ul id="footer" class="ui-tiled ui-border-t">
			<li data-href="index.html" class="ui-border-r">
				<div>查询购票</div>
			</li>
			<li data-href="order.html" class="ui-border-r">
				<div>订单查询</div>
			</li>
			<li data-href="member.html">
				<div>会员中心</div>
			</li>
		</ul>
	</footer>
	<section class="ui-container">
		<section id="start-station-container" class="ui-selector">
			<header>
				<h3></h3>
			</header>
			<ul id="start-station-list" class="ui-list ui-list-text ui-list-cover ui-border-tb">
			</ul>
		</section>
	</section>

<script>
(function (){
	/* -------------------- 根据localStorage中的值，初始化界面 -------------------- */
	
	// 查询并加载起点站
	ajax({
		url: appConfig.baseUrl + "/BusTicketInfo.go",
		data: {
			method: "querySellStationInfo"
		},
		failMessage: "查询失败，请重试",
		success: function(data) {
			if(!!data.rows) {
				$("#start-station-list").empty();
				
				var recentRoutes = getRecentRoutes();
				clearRecentRoutes();
				for(var i=0; i<data.rows.length; i++) {
					var record = data.rows[i];
					$("#start-station-list").append(
						"<li class='ui-border-t' start-station-id='" + record.SellStationId + "' sell-day='" + record.SellDay + "'>" + record.SellStationName + "</li>"
					);
					
					// 更新常用线路中的sellDay
					for(var j=0; j<recentRoutes.length; j++) {
						var r = recentRoutes[j];
						if(r.startStationId===record.SellStationId) {
							r.sellDay = record.SellDay
						}
						
						addRecentRoutes(r);
					}
				}
			}
		}
	});
	
	// 起点站列表点击事件
    $(document).on("tap", "#start-station-list li", function(){
		var startStationId = $(this).attr("start-station-id");
		var startStationName = $(this).html();
		var sellDay = $(this).attr("sell-day");
		sessionStorage.startStationId = startStationId;
		sessionStorage.startStationName = startStationName;
		sessionStorage.sellDay = sellDay;
		location.href = "index.html";
	});
	
	// 底部主菜单初始化
	initFooter("#footer");
})();

// 新增常用线路
function addRecentRoutes(route) {
	var recentRouteList = getRecentRoutes();
	for(var i=0; i<recentRouteList.length; i++) {
		var r = recentRouteList[i];
		if(r.routeId===route.routeId) {
			return;
		}
	}
	recentRouteList.push(route);
	localStorage.setItem("recentRoutes", JSON.stringify(recentRouteList));
}

// 清空常用线路
function clearRecentRoutes() {
	localStorage.removeItem("recentRoutes");
		$("#recent-route-container").hide();
		$("#recent-route-list").empty();
}

// 从本地存储获取常用线路
function getRecentRoutes() {
	var recentRoutes = localStorage.getItem("recentRoutes");
	if(!!recentRoutes) {
		return JSON.parse(recentRoutes);
	} else {
		return [];
	}
}
</script>
</body>
</html>